package com.googlecode.felicalib.sample.client;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * java application (JNLP).
 * 'felicalib.dll'へアクセスを表示するための定義.
 * このソースは[システム開発サンプル]から流用したものです。
 * http://yuuhayashi.blogspot.com/search/label/FeliCa
 *
 * @author o.tanaka
 */

public class FelicaJFrame extends JFrame {

    /**
     * long serialVersionUID
     */
    private static final long serialVersionUID = 8064531557020146279L;

    private final JTextField idTextField;
    private final JTextField pmTextField;

    /**
     * コンストラクタ.
     */
    public FelicaJFrame() {
        super("'FeliCa' IDm");

        // 終了時処理の追加
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(final WindowEvent e) {
                FelicaJFrame.this.setVisible(false);
            }
        });

        this.setLayout(new GridLayout(4, 1));
        this.idTextField = new JTextField(16);
        final JPanel idPanel = new JPanel();
        idPanel.setLayout(new FlowLayout());
        idPanel.add(new JLabel("IDm "));
        idPanel.add(this.idTextField);
        this.add(idPanel);
        this.pmTextField = new JTextField(16);
        final JPanel pmPanel = new JPanel();
        pmPanel.setLayout(new FlowLayout());
        pmPanel.add(new JLabel("PMm "));
        pmPanel.add(this.pmTextField);
        this.add(pmPanel);
        this.add(new JLabel("Polling...."));
        this.add(new JLabel("Copyright (c) 2009 by Tanaka,Osamu."));
        this.pack();

        // IDが全部表示されないこともあるため、幅を変更する
        final Dimension dim = this.getPreferredSize();
        if (dim.getWidth() < 300) {
            this.setPreferredSize(new Dimension(300, (int) dim.getHeight()));
            this.pack();
        }

        final Dimension screenSize =
            Toolkit.getDefaultToolkit().getScreenSize();
        final Dimension frameSize = this.getSize();
        this.setLocation((screenSize.width - frameSize.width) / 2,
                (screenSize.height - frameSize.height) / 2);
    }

    /**
     * @param aIdm
     *            the idm to set
     */
    public final void setIDm(final String aIdm) {
        this.idTextField.setText(aIdm);
    }

    /**
     * @param aPmm
     *            the pmm to set
     */
    public final void setPMm(final String aPmm) {
        this.pmTextField.setText(aPmm);
    }
}
